/**
 * Created by yunpeng on 2015/3/26.
 */
angular.module('starter.centerSrvices', [])
    .factory('CenterService',function($http){
        String.prototype.trim = function(){
            return this.replace(/(^\s*)|(\s*$)/g, "");
        }
        return {
            postFileForm:function(params , scope,callback){
                var xhr = new XMLHttpRequest();
                xhr.open("POST" ,params.url , true);
                xhr.setRequestHeader('Authorization','Bearer ' +  localStorage.getItem('token'))//$window.sessionStorage.token
                xhr.upload.addEventListener("progress", function(evt){
                    if (evt.lengthComputable) {
                        var percentComplete = Math.round(evt.loaded * 100 / evt.total);
                        //document.getElementById('toast-err').innerHTML = '已上传 '+percentComplete.toString() + '%';
                        scope.loadingTip = '资料正在上传  '+percentComplete.toString() + '%';
                        scope.$apply();
                    }
                }, false);
                xhr.send(params.data);
                xhr.onload = function(e) {
                    if (this.status == 200) {
                        scope.loadingTip = '资料已经上传，请等待人工审核';
                        scope.$apply();
                        setTimeout(function(){
                            scope.loadingTip = '';
                            scope.$apply();
                        } ,5000)
                        callback(JSON.parse(this.responseText))
                    }
                };
            }
            ,info:{
                id:''
                ,nickname:''
                ,address:''
                ,introduce:''
            },
            getAuthState:function(id){
                return $http({ //返回promise对象
                    method:'POST',
                    url:'/users/getAuthState',
                    data:{
                        id:id,
                        type:1
                    }
                });
            }

        }
    })
    ;